package jeecg.business.service.impl.keyword;

import java.util.ArrayList;
import java.util.List;

import jeecg.business.entity.enter.EnterEntity;
import jeecg.business.entity.keyword.EnterWordEntity;
import jeecg.business.entity.keyword.KeywordEntity;
import jeecg.business.service.keyinfo.KeyinfoServiceI;
import jeecg.business.service.keyword.KeywordServiceI;
import jeecg.business.vo.CommonAnalysisVo;
import jodd.util.StringUtil;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("keywordService")
@Transactional
public class KeywordServiceImpl extends CommonServiceImpl implements KeywordServiceI {
	@Autowired
	KeyinfoServiceI keyInfoService;
	
	public String [] getSelectedId(String enterid){
		String sql = " select wordid from nms_enter_word where enterid = '"+enterid+"'";
		List<String> idlist = getJdbcTemplate().queryForList(sql, String.class);
		if(idlist != null){
			return idlist.toArray(new String[0]);
		}
		return null;
	}
	public void assignKeyword(String enterid , String keys){
		String [] keyarray = keys.split(",");
		EnterWordEntity enterWord = null;
		for(String key : keyarray){
			if(getJdbcTemplate().queryForInt(" select count(1) from nms_enter_word where enterid = '"+enterid+"' and wordid = '"+key+"'")==0){
				enterWord = new EnterWordEntity();
				enterWord.setEnterid(enterid);
				enterWord.setWordid(key);
				save(enterWord);
			}
		}
	}
	public void delKeyword(String enterid , String keys){
		keys = "'"+keys.replace(",", "','")+"'";
		getJdbcTemplate().execute(" delete from nms_enter_word where enterid = '"+enterid+"' and wordid in ("+keys+")");
	}
	
	public List<KeywordEntity> findVail(){
		String hql = " from KeywordEntity where status = '1' ";
		return findByQueryString(hql);
	}
	
	public List<KeywordEntity> find(String enterid){
		List<KeywordEntity> klist = findByQueryString(" select k from KeywordEntity k  where k.qyid = '"+enterid+"' " );
//		List<KeywordEntity> klist = findByQueryString(" select k from KeywordEntity k , EnterWordEntity w where w.enterid = '"+enterid+"' and k.id = w.wordid" );
		return klist;
	}
	
	public KeywordEntity get(String enterid , String keyword){
		List<KeywordEntity> klist = findByQueryString(" select k from KeywordEntity k  where k.qyid = '"+enterid+"' and keyword = '"+keyword+"'" );
//		List<KeywordEntity> klist = findByQueryString(" select k from KeywordEntity k , EnterWordEntity w where w.enterid = '"+enterid+"' and k.id = w.wordid" );
		if(klist == null || klist.size() == 0)return null;
		return klist.get(0);
	}
	
	public List<KeywordEntity> find(String qyid , String lx , String keyword){
		String hql = " from KeywordEntity where qyid = '"+qyid+"'";
		if(StringUtil.isNotEmpty(lx)){
			hql += " and lx = '"+lx+"'";
		}
		
		if(StringUtil.isNotEmpty(keyword)){
			hql += " and keyword = '"+keyword+"'";
		}
		return findByQueryString(hql);
	}
	
	public List<String> listProvince(String enterid){
		String sql = " select distinct sf from nms_key_info where qyid = '"+enterid+"' and sf != '其他' ";
		List<String> list = getJdbcTemplate().queryForList(sql , String.class);
		if(list != null && list.size()>0) list.add("其他");
		return list;
	}
}